package com.zkdcloud.service;

import com.zkdcloud.domain.Order;
import com.zkdcloud.dto.OrderDisplayBean;
import com.zkdcloud.dto.QueryOrderBean;
import com.zkdcloud.dto.TerminalCartInfo;
import com.zkdcloud.dto.TerminalOrder;
import com.zkdcloud.repository.OrderDao;
import com.zkdcloud.repository.PaymentPlatformDao;
import com.zkdcloud.util.DateUtil;
import com.zkdcloud.util.RandomUtil;
import org.jfaster.mango.plugin.page.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zk123
 * @version 2018/5/23
 */
@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private BusinessService businessService;

    @Autowired
    private CartService cartService;

    @Autowired
    private PaymentPlatformDao paymentPlatformDao;

    @Autowired
    private UserService userService;

    /**
     * 添加订单
     *
     * @param order order
     */
    public String add(Order order) {
        order.setId(RandomUtil.getUUID());
        order.setCreateTime(System.currentTimeMillis()/1000);
        if (StringUtils.isEmpty(order.getAddress())) {
            order.setAddress("");
        }
        if (StringUtils.isEmpty(order.getRemark())) {
            order.setRemark("");
        }
        orderDao.add(order);
        return order.getId();
    }

    /**
     * 根据id获取
     *
     * @param id id
     * @return order
     */
    public Order getOne(String id) {
        return orderDao.getOne(id);
    }

    /**
     * 获取终端订单展示
     *
     * @param id id
     * @return
     */
    public TerminalOrder getTerminalOrder(String id) {
        Order order = getOne(id);
        TerminalCartInfo terminalCartInfo = cartService.getBaseCartInfo(order.getCartId());

        TerminalOrder result = new TerminalOrder();
        BeanUtils.copyProperties(order, result);
        result.setBusinessInfo(businessService.get(order.getBusinessId()));
        result.setCartInfo(terminalCartInfo);
        result.setOrderNum(order.getId());
        result.setOrderPayMethod(Integer.valueOf(order.getPayMethod()));
        return result;
    }

    /**
     * 根据uid获取列表
     *
     * @param uid uid
     * @return list
     */
    public List<TerminalOrder> getTerminalOrderListByUid(String uid) {
        List<Order> orderList = orderDao.getAllByUid(uid);
        List<TerminalOrder> result = new ArrayList<>();
        for (Order order : orderList) {
            result.add(getTerminalOrder(order.getId()));
        }
        return result;
    }

    /**
     * 获取商家的id
     *
     * @param queryOrderBean 查询参数
     * @return
     */
    public List<OrderDisplayBean> getDisplayOrderListByBid(QueryOrderBean queryOrderBean, Page page){
       List<Order> orderList= orderDao.getAllByBid(queryOrderBean,page);
       List<OrderDisplayBean> result = new ArrayList<>();
        for (Order order : orderList) {
            OrderDisplayBean orderDisplayBean = new OrderDisplayBean();
            BeanUtils.copyProperties(order,orderDisplayBean);
            orderDisplayBean.setBusiness(businessService.get(order.getBusinessId()));
            orderDisplayBean.setUser(userService.getById(order.getUserId()));
            orderDisplayBean.setProducts(cartService.getCartProductByCartId(order.getCartId()));
            result.add(orderDisplayBean);
        }
        return result;
    }

    public OrderDisplayBean getDisplayOrderOne(String id){
        OrderDisplayBean result = new OrderDisplayBean();
        Order order = orderDao.getOne(id);
        BeanUtils.copyProperties(order,result);
        result.setBusiness(businessService.get(order.getBusinessId()));
        result.setUser(userService.getById(order.getUserId()));
        result.setProducts(cartService.getCartProductByCartId(order.getCartId()));
        return result;
    }
    /**
     * 修改订单状态
     *
     * @param orderId orderId
     * @param status status
     */
    public void changeStatus(String orderId,String status){
        orderDao.changeStatus(orderId,status);
    }
}
